package com.zhenwei.zscm.repo;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.base.MybatisWrapper;
import com.zhenwei.zscm.common.base.RestMsg;
import com.zhenwei.zscm.common.base.UserContext;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.OrderEntity;
import com.zhenwei.zscm.mapper.IOrderMapper;
import com.zhenwei.zscm.mappings.OrderMapping;
import com.zhenwei.zscm.model.dto.order.OrderDTO;
import com.zhenwei.zscm.model.enums.OrderStatusEnum;
import com.zhenwei.zscm.model.vo.order.CreateOrderVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author DuckyYang
* @description 针对表【order】的数据库操作Service实现
* @createDate 2025-05-22 09:40:50
*/
@Service
public class OrderRepo extends ServiceImpl<IOrderMapper, OrderEntity> {

    public boolean create(CreateOrderVO vo) {
        vo.setGuid(IdUtil.simpleUUID());
        OrderEntity e = OrderMapping.INSTANCE.vo2Entity(vo);
        e.setOrderStatus(OrderStatusEnum.SUBMITTED.name());
        e.setOrderStatusName(OrderStatusEnum.SUBMITTED.getText());
        return this.save(e);
    }

    public boolean checkOrderNumberExists(String orderNumber){
        var q = new LambdaQueryWrapper<OrderEntity>()
                .eq(OrderEntity::getOrderNumber, orderNumber)
                .eq(OrderEntity::getDeleted,false);
        return this.exists(q);
    }

    public boolean update(CreateOrderVO vo) {
        var u = new LambdaUpdateWrapper<OrderEntity>()
                .eq(OrderEntity::getGuid, vo.getGuid())
                .eq(OrderEntity::getDeleted, false)
                .set(OrderEntity::getCustomerCode, vo.getCustomerCode())
                .set(OrderEntity::getCustomerName, vo.getCustomerName())
                .set(OrderEntity::getSiteName, vo.getSiteName())
                .set(OrderEntity::getContactor, vo.getContactor())
                .set(OrderEntity::getTelephone, vo.getTelephone())
                .set(OrderEntity::getAddress,vo.getAddress())
                .set(OrderEntity::getReceiveDate, vo.getReceiveDate())
                .set(OrderEntity::getReceiveNumber, vo.getReceiveNumber())
                .set(OrderEntity::getComment, vo.getComment());
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }

    public OrderDTO get(String orderId){
        var q = new LambdaQueryWrapper<OrderEntity>()
                .eq(OrderEntity::getGuid, orderId)
                .eq(OrderEntity::getDeleted,false);
        List<OrderEntity> list = this.list(q);
        return Linq.getFirst(list, OrderMapping.INSTANCE::entity2Dto);
    }

    public boolean delete(String orderId){
        var u = new LambdaUpdateWrapper<OrderEntity>()
                .eq(OrderEntity::getGuid, orderId)
                .eq(OrderEntity::getDeleted,false)
                .set(OrderEntity::getDeleted,true);
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }

    public boolean updateStatus(String orderId, OrderStatusEnum status) {
        var u = new LambdaUpdateWrapper<OrderEntity>()
                .eq(OrderEntity::getGuid, orderId)
                .eq(OrderEntity::getDeleted,false)
                .set(OrderEntity::getOrderStatus,status.name())
                .set(OrderEntity::getOrderStatusName,status.getText());
        return this.update(u);
    }
    public boolean finishOrder(String orderId) {
        var u = new LambdaUpdateWrapper<OrderEntity>()
                .eq(OrderEntity::getGuid, orderId)
                .eq(OrderEntity::getDeleted,false)
                .set(OrderEntity::getOrderStatus,OrderStatusEnum.FINISHED.name())
                .set(OrderEntity::getOrderStatusName,OrderStatusEnum.FINISHED.getText())
                .set(OrderEntity::getCompleteDate, new Date());
        return this.update(u);
    }

    public String checkOrderStatus(String orderId) {
        var order = this.get(orderId);
        if(order == null) {
            return RestMsg.ORDER_NOT_EXISTS;
        }
        if(UserContext.isAdmin()) {
            return "";
        }
        if(OrderStatusEnum.FINISHED.name().equals(order.getOrderStatus())) {
            return RestMsg.ORDER_FINISHED;
        }
        if(OrderStatusEnum.STOP.name().equals(order.getOrderStatus())) {
            return RestMsg.ORDER_STOPPED;
        }
        return "";
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<OrderDTO> orderList) {
        List<OrderEntity> entityList = Linq.mapList(orderList, OrderMapping.INSTANCE::dto2Entity);
        if(Linq.isEmpty(entityList)) {
            return false;
        }
        return this.saveBatch(entityList);
    }

    public List<?> countByState(){
        return baseMapper.countByState();
    }

    public List<?> countByProductRepairState(){
        return baseMapper.countByProductRepairState();
    }

    public List<?> customerSearch(List<String> keys) {
        return baseMapper.customerSearch(keys);
    }
}




